<template>
    <div class="app">
        <header class="header">
            <div class="project-name">后台管理系统</div>
            <div class="user-profile">
                <img :src="store.state.userInfo.pic"
                     alt="User Avatar" class="avatar">
                <div class="user-name">{{ store.state.userInfo.username }}</div>
                <button @click="logout">注销登录</button>
            </div>
        </header>
        <div class="main">
            <div class="sidebar">
                <el-menu :router="true" default-active="0">
                    <el-menu-item index="0" :route="{name:'index'}">首页</el-menu-item>
                    <el-menu-item index="1" :route="{name:'user'}">用户管理</el-menu-item>
                    <el-menu-item index="2" :route="{name:'client'}">客户端管理</el-menu-item>
                    <el-menu-item index="3" :route="{name:'scope'}">授权范围管理</el-menu-item>
                </el-menu>
            </div>
            <div class="content">
                <!-- 根据路由展示不同内容 -->
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>

<script setup>
import {onMounted} from "vue";
import router from "/@/router/index.js";
import {PATH_URL_LOGIN} from "/@/assets/constant.js";
import {TOKEN_KEY, USER_INFO_KEY} from "../assets/constant.js";
import store from "/@/store/index.js";

const logout = () => {
    localStorage.removeItem(TOKEN_KEY)
    localStorage.removeItem(USER_INFO_KEY)
    router.push(PATH_URL_LOGIN)
}

onMounted(() => {
    let userStr = localStorage.getItem(USER_INFO_KEY);
    let userObj = JSON.parse(userStr)
    // 使用store，生命周期高些
    store.state.userInfo.username = userObj.username
    store.state.userInfo.pic = userObj.picture
})
</script>

<style scoped>
.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background-color: #f0f0f0;
}

.project-name {
    font-size: 20px;
    font-weight: bold;
}

.user-profile {
    display: flex;
    align-items: center;
}

.user-name {
    margin-right: 10px;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 10px;
}

.main {
    display: flex;
    flex: 1;
}

.sidebar {
    width: 200px;
    background-color: #f0f0f0;
    padding: 10px;
}

.content {
    flex: 1;
    padding: 10px;
}
</style>